<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询预约管理</el-breadcrumb-item>
		</el-breadcrumb>


		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true" :model="queryData">
				<el-form-item label="用户昵称">
					<el-input v-model="queryData.nickname" @clear="queryList" clearable placeholder="请输入用户昵称"></el-input>
				</el-form-item>
				<el-form-item label="咨询师名字">
					<el-input v-model="queryData.consultantName" @clear="queryList" clearable placeholder="请输入咨询师名字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<!-- <el-button type="primary" @click="add">新增预约</el-button> -->
					<el-button type="danger" @click="removBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>

		<!-- 数据表格区域 -->
		<el-table :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="80"></el-table-column>
			<el-table-column prop="bookingTime" label="咨询预约时间" ></el-table-column>
			<el-table-column prop="bookingCondition" label="情况说明"></el-table-column>
			<el-table-column prop="nickname" label="用户昵称"></el-table-column>
			<el-table-column prop="consultantName" label="咨询师姓名"></el-table-column>
		
			<el-table-column prop="status" label="状态"  >
				<template slot-scope="scope" >
					<el-tag v-if="scope.row.status == 0">已完成</el-tag>
					<el-tag type="danger" v-if="scope.row.status == 1">未完成</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="210">
				<template slot-scope="scope">
					<!-- <el-button size="mini" type="primary" @click="edit(scope.row)">编辑</el-button> -->
					<el-button size="mini" type="danger" @click="remove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryData.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryData.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>

		<!--  
			el-dialog: 对话框组件
				title: Dialog 的标题
				visible: 是否显示 Dialog，支持 .sync 修饰符
		-->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="addOrUpdateData" label-width="100px" ref="addOrUpdateForm">
				
				<el-form-item label="状态">
					<el-select v-model="addOrUpdateData.status" placeholder="请选择状态">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">确 定</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				// 查询参数对象
				queryData: {
					nickname: '',
					consultantName: '',
					pageNum: 1,
					pageSize: 10
				},
				// 状态列表
				options: [{
					value: '0',
					label: '已完成'
				}, {
					value: '1',
					label: '未完成'
				}],
				total: 0,
				title: '新增用户', // 对话框的标题
				dataList: [], // 当前页数据列表
				dialogFormVisible: false,
				// 新增和修改数据集
				addOrUpdateData: {
					id: "",
					bookingTime: "",
					bookingCondition: "",
					nickname: "",
					consultantName: "",
					status: "0"
				},
				ids: []
			}
		},
		methods: {
			handleSelectionChange(val) {
				// val是选中的数组对象, map() 方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值。
				this.ids = val.map(item => item.id);
			},
			removBatch(){
				if(this.ids.length == 0){
					this.$message('请选择要删除的数据!');
				} else {
					this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.$ajax.delete('booking?ids=' + this.ids).then(resp => {
							if (resp.code == 200) {
								this.$message.success(resp.message)
								// 重新查询
								this.queryList()
							} else {
								this.$message.error(resp.message)
							}
					
						})
					
					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
				}
			},
			// 每页条目数改变的回调
			handleSizeChange(val) {
				// 修改查询参数的pageSize
				this.queryData.pageSize = val;
				// pageSize改变，默认查询第一页
				this.queryData.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			// 页码发生改变的回调
			handleCurrentChange(val) {
				// 修改查询参数的pageNum
				this.queryData.pageNum = val;
				// 重新查询
				this.queryList()
			},
			// 封装查询用户列表数据的函数
			queryList() {
				// 因为自定义axios已经定义了baseUrl，所以这里直接写相对路径即可
				this.$ajax.get('booking/page', {
					params: this.queryData
				}).then(resp => {
					if (resp.code == 200) {
						this.dataList = resp.data.list
						this.total = resp.data.total
					} else {
						this.$message.error('用户数据加载失败')
					}
				})
			},
			// 对话框的取消按钮事件函数: 清空对话的数据
			cancle() {
				this.dialogFormVisible = false
				this.addOrUpdateData = {
					id: "",
					bookingTime: "",
					bookingCondition: "",
					nickname: "",
					consultantName: "",
					status: "0"
				}
			},
			// 对话框的确定按钮事件函数
			confirm() {
				this.dialogFormVisible = false

				if (this.addOrUpdateData.id == "") { // 执行新增请求
					this.$ajax.post('booking', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				} else {
					this.$ajax.put('booking', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				}
				this.cancle()
			},
			// 新增用户
			add() {
				this.dialogFormVisible = true;
				this.title = '新增用户'
			},
			// 修改用户
			edit(row) {
				this.title = '编辑用户'
				// 根据id实时查询数据，然后进行回显
				this.$ajax.get('booking/' + row.id).then(resp => {
					this.addOrUpdateData = resp.data
				})
				this.dialogFormVisible = true;
			},
			// 删除用户
			remove(row) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$ajax.delete('booking/' + row.id).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}

					})

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		},
		// 页面初始化的时候，加载第一页的数据
		mounted() {
			this.queryList()
		}
	}
</script>

<style scoped>
	.el-breadcrumb {
		margin-bottom: 15px;
		border-bottom: 1px solid #eee;
		padding-bottom: 15px;
	}

	.el-pagination {
		margin-top: 15px
	}


	.el-col>.el-select {
		width: 100%;
	}
</style>